package com.cqut.forum.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.cqut.forum.dao.ArticleDao;
import com.cqut.forum.dao.ModuleDao;
import com.cqut.forum.entity.Article;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.ActiveService;
import com.cqut.forum.util.MapKey;

/**
 * article service实现类
 * @author yangxun
 */
@Controller
public class ActiveServiceImp implements ActiveService {
	@Resource
	private ArticleDao articleDao;
	@Resource
	private ModuleDao moduleDao;
	public Article addActive(Article article) {
		article = articleDao.save(article);
		
		return article;
	}
	@Override
	public List<Map<String, Object>> getByOwnerId(int userId,int index,int size) {
		int moduleIndex = moduleDao.getModuleIndex("博客");
		List<Map<String, Object>> articles = articleDao.getByOwnerId(moduleIndex,userId,index,size);
		return articles;
	}
	@Override
	public List<Map<String, Object>> getByOwnerId(int userId, String keyWords, int index,
			int size) {
		int moduleIndex = moduleDao.getModuleIndex("博客");
		List<Map<String, Object>> articles = articleDao.getByOwnerId(moduleIndex,keyWords,userId,index,size);
		return articles;
	}
	@Override
	public Article getArticle(int articleId) {
		Article article = articleDao.get(articleId, Article.class);
		//由于Hibernate懒加载机制，强制加载数据
		return article;
	}
	@Override
	public Article updateArticle(Article article) {
		article = articleDao.update(article);
		return article;
	}
	@Override
	public String getArticleGroupName(int articleId) {
		String name = articleDao.getArticleGroupName(articleId);
		return name;
	}
	/**
	 * 获取一篇博客的访问者
	 */
	@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> getArticleVisitor(int articleId) {
		List<Map<String, Object>> visitors = articleDao.getArticleVisitor(articleId);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> m:visitors){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", m.get("user_id"));
			map.put("userPhoto", m.get("user_photo"));
			map.put("userName", m.get("user_name"));
			map.put("visitDate", ((Date)m.get("article_reply_time")).toLocaleString());
			result.add(map);
		}
		
		return result;
	}

	@Override
	public List<User> getHotArticleUser(int index, int size) {
		List<User> result = articleDao.getHotArticleUser(index,size);
		return result;
	}
	@Override
	public List<Map<String, Object>> getHotArticle(int index, int size) {
		List<Map<String, Object>> articles = articleDao.getHotArticle(index, size);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> map:articles){
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("articleName", map.get("article_name"));
			m.put("articleKeyWords", map.get("article_key_words"));
			m.put("articleId", map.get("article_id"));
			m.put("replyTimes", map.get("review_times"));
			m.put("userName", map.get("user_name"));
			m.put("userPhoto", map.get("user_photo"));
			
			result.add(m);
		}
		
		return result;
	}
	@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> getLastArticle(int index, int size,int moduleId) {
		
		List<Map<String, Object>> articles = articleDao.get(index,size,moduleId);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> m : articles){
			Map<String, Object> map = new HashMap<String, Object>();
			
			map.put("articleName", m.get("article_name")==null?m.get("user_name"):m.get("article_name"));
			map.put("publishTime", ((Date)m.get("publish_time")).toLocaleString());
			map.put("replyTimes", m.get("review_times"));
			map.put("articleId", m.get("article_id"));
			map.put("userName", m.get("user_name"));
			map.put("userId", m.get("user_id"));
			
			result.add(map);
		}
		
		return result;
	}
	@Override
	public List<Article> getByGroupId(int groupId, int index, int size) {
		int moduleIndex = moduleDao.getModuleIndex("博客");
		List<Article> articles = articleDao.getByGroupId(moduleIndex,groupId,index,size);
		return articles;
	}
	@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> getArticleByCase(int userId,int moduleId,int index,int size,String option) {
		List<Map<String, Object>> result = articleDao.getArticleByCase(userId,moduleId,index,size,option);
		
		List<Map<String, Object>> articles = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> m:result){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("type", m.get("article_password"));
			map.put("content", m.get("article_contents"));
			map.put("time", ((Date)m.get("publish_time")).toLocaleString());
			map.put("reply", m.get("review_times"));
			map.put("targetId", m.get("article_id"));
			articles.add(map);
		}
		return articles;
	}
	@Override
	public List<Map<String, Object>> getModuleVisitors(int userId,
			int moduleId, int index, int size) {
		List<Map<String, Object>> visitors = articleDao.getModuleVisitors(userId,moduleId,index,size);
		
		return visitors;
	}
	
	@Override
	public boolean haveReadMyArticle(int userId, String moduleName) {
		int moduleId = moduleDao.getModuleIndex(moduleName);
		boolean result = articleDao.haveReadMyArticle(userId,moduleId);
		return result;
	}
	
	/************get and set method**************/
	public ArticleDao getArticleDao() {
		return articleDao;
	}
	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}
	@Override
	public int count(int userId, String moduleName) {
		int moduleId = moduleDao.getModuleIndex(moduleName);
		int count = articleDao.count(userId,moduleId);
		return count;
	}
	@Override
	public boolean deleteArticel(int userId, int targetId) {
		articleDao.updateArticleState(userId,targetId, false);
		
		return true;
	}
	@Override
	public Map<String, Object> getNext(int articleId) {
		Map<String, Object> result = articleDao.getNext(articleId);
		result = MapKey.changeMapKey(result);
		return result;
	}
	@Override
	public Map<String, Object> getPrevious(int articleId) {
		Map<String, Object> result = articleDao.getPrevious(articleId);
		result = MapKey.changeMapKey(result);
		return result;
	}
}
